import { changeUserInfo, updateDataApi } from "@/api/login"
import { useRef, useState } from "react"
import { useImperativeHandle } from "react"
import { forwardRef } from "react"
type AvtRefType = {
    target: string;
    changeImg: () => void;
    Imgs:string
}
export const ChangeAvt = forwardRef<AvtRefType,{dianji:Function,k?:boolean}>(({dianji,k=true},ref)=>{
    const changes = async()=>{
        const file = htmls.current.files?.[0]
        console.log(file);
        
        const data = new FormData()
        data.append('file',file!)
        console.log(data);
        
        let res = await updateDataApi(data)
        if(res.code == 200){
            dianji(res.path)
            if(k){
                let y = await changeUserInfo(res.result)
                console.log(y);
            }

            
            
        }
        
    }
    const [img,setImgs] = useState('')
    const htmls = useRef<HTMLInputElement>({} as HTMLInputElement)
    useImperativeHandle(ref,()=>{
        return {
            target:"changeImg",
            Imgs:img,
            changeImg(){
                    htmls.current.click()
            }
        }
    })
    return (
        <div>
            <input type="file" style={{display:'none'}} ref={htmls} onChange={changes}/>
        </div>
    )

})